package com.mr.cai.main.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.mr.cai.main.R;

import com.mr.cai.main.activity.MrCaiSearchActivity;

import com.mr.cai.main.activity.ClassifyActivity;

import com.mr.cai.main.adapter.GroupFragmentAdapter;
import com.mr.cai.main.bean.GroupInfoBean;
import com.mr.cai.main.bean.GroupInfoBeanList;
import com.mr.cai.main.httpApi.MrCaiHttpApi;
import com.mr.cai.main.parse.GroupInfoBeanListParse;
import com.mr.cai.main.utils.CommonCoreUtil;

import java.util.ArrayList;

import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.functions.Action1;

/**
 * Created by hai8108 on 16/2/10.
 */
public class GroupPageFragment extends BaseFragment {
    private GridView groupGridView;
    private GroupFragmentAdapter adapter;
    private TextView searchTv;
    private ArrayList<GroupInfoBean> groupInfoBeans;

    @Override
    public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mPublicLayout = CommonCoreUtil.createPage(getActivity(), R.layout
                .fragment_page_group_layout);
        return mPublicLayout;
    }

    @Override
    public void onViewCreated (View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        findView();
        setListener();
        requestGroupData("groupId");
    }

    private void setListener() {
        groupGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                try {
                    GroupInfoBean groupInfoBean= (GroupInfoBean) adapterView.getItemAtPosition(i);
                    if(groupInfoBean==null){
                        return;
                    }
                    Intent intent=new Intent(getActivity(),ClassifyActivity.class);
                    getActivity().startActivity(intent);
                }catch (Exception e){

                }
            }
        });
    }

    private void findView () {
        searchTv = (TextView) mPublicLayout.findViewById(R.id.mr_cai_search);
        CommonCoreUtil.setClickAction(searchTv, new Action1() {
            @Override
            public void call (Object o) {
                MrCaiSearchActivity.launch(getActivity());
            }
        });
        groupGridView = (GridView) mPublicLayout.findViewById(R.id.mr_group_gridview);
        adapter = new GroupFragmentAdapter(getActivity());
        groupGridView.setAdapter(adapter);

    }

    @Override
    public void onActivityCreated (Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    private Subscription groupSubscription;

    private void requestGroupData (String parentId) {
        Observable<GroupInfoBeanList> authCodeObservable = MrCaiHttpApi.requestGroupList(new GroupInfoBeanListParse());
        groupSubscription = authCodeObservable.observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call () {
                        mPublicLayout.loading(true);
                    }
                })
                .subscribe(new Subscriber<GroupInfoBeanList>() {
                    @Override
                    public void onCompleted () {
                        mPublicLayout.finish();
                    }
                    @Override
                    public void onError (Throwable e) {
                        mPublicLayout.dataError(false);
                    }
                    @Override
                    public void onNext (GroupInfoBeanList mGroupInfoBeanList) {
                        if(mGroupInfoBeanList.isResult() && mGroupInfoBeanList.getCode() == 1){
                            adapter.setList(mGroupInfoBeanList.getData());
                        } else {
                            mPublicLayout.dataNull(mGroupInfoBeanList.getMsg());
                        }
                    }
                });
    }

    @Override
    public void onDestroy () {
        super.onDestroy();
        if (groupSubscription != null) {
            groupSubscription.unsubscribe();
            groupSubscription = null;
        }
    }
}
